package com.ssy.lingxi.member.merchant.serviceimpl.web;

import com.ssy.lingxi.common.constant.basic.EnableDisableStatus;
import com.ssy.lingxi.common.exception.BusinessException;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.member.merchant.entity.MemberDO;
import com.ssy.lingxi.member.merchant.entity.MemberRoleDO;
import com.ssy.lingxi.member.merchant.model.bo.AuthBO;
import com.ssy.lingxi.member.merchant.model.bo.ButtonBO;
import com.ssy.lingxi.member.merchant.model.constant.MemberUserAuthTypeEnum;
import com.ssy.lingxi.member.merchant.model.vo.basic.request.AuthTreeGetButtonVO;
import com.ssy.lingxi.member.merchant.model.vo.basic.response.AuthTreeButtonVO;
import com.ssy.lingxi.member.merchant.model.vo.basic.response.AuthTreeMenuButtonCheckIdVO;
import com.ssy.lingxi.member.merchant.model.vo.basic.response.AuthTreeMenuVO;
import com.ssy.lingxi.member.merchant.model.vo.maintenance.request.*;
import com.ssy.lingxi.member.merchant.model.vo.maintenance.response.MemberRoleGetButtonVO;
import com.ssy.lingxi.member.merchant.model.vo.maintenance.response.PlatformMemberRoleGetVO;
import com.ssy.lingxi.member.merchant.model.vo.maintenance.response.PlatformMemberRoleQueryVO;
import com.ssy.lingxi.member.merchant.repository.MemberRepository;
import com.ssy.lingxi.member.merchant.repository.MemberRoleRepository;
import com.ssy.lingxi.member.merchant.service.base.IBaseAuthService;
import com.ssy.lingxi.member.merchant.service.base.IBaseMemberCacheService;
import com.ssy.lingxi.member.merchant.service.feign.IWorkflowFeignService;
import com.ssy.lingxi.member.merchant.service.web.IPlatformMemberRoleService;
import com.ssy.lingxi.member.merchant.utils.AuthBOUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 平台后台 - 会员自定义角色服务接口实现类
 *
 * @author 万宁
 * @version 2.0.0
 * @date 2020-06-30
 */
@Service
public class PlatformMemberRoleServiceImpl implements IPlatformMemberRoleService {
    @Resource
    private MemberRepository memberRepository;

    @Resource
    private MemberRoleRepository memberRoleRepository;

    @Resource
    private IBaseMemberCacheService memberCacheService;

    @Resource
    private IBaseAuthService baseAuthService;

    @Resource
    private IWorkflowFeignService workflowFeignService;

    /**
     * 分页、模糊查询会员角色
     *
     * @param headers HttpHeaders信息
     * @param pageVO  接口参数
     * @return 操作结果
     */
    @Override
    public Wrapper<PageData<PlatformMemberRoleQueryVO>> pageMemberRole(HttpHeaders headers, MemberRolePageVO pageVO) {
        UserLoginCacheDTO loginUser = memberCacheService.needLoginFromManagePlatform(headers);
        MemberDO memberDO = memberRepository.findById(loginUser.getMemberId()).orElse(null);
        if (memberDO == null) {
            return Wrapper.fail(ResponseCode.MC_MS_MEMBER_DOES_NOT_EXIST);
        }

        Pageable page = PageRequest.of(pageVO.getCurrent() - 1, pageVO.getPageSize(), Sort.by("id").ascending());
        Specification<MemberRoleDO> specification = (Specification<MemberRoleDO>) (root, query, criteriaBuilder) -> {
            List<Predicate> list = new ArrayList<>();

            Join<Object, Object> memberJoin = root.join("member", JoinType.LEFT);
            list.add(criteriaBuilder.equal(memberJoin.get("id").as(Long.class), loginUser.getMemberId()));

            if(pageVO.getStatus() != null) {
                list.add(criteriaBuilder.equal(root.get("status").as(Integer.class), pageVO.getStatus()));
            }

            if(StringUtils.hasLength(pageVO.getRoleName())) {
                list.add(criteriaBuilder.like(root.get("roleName").as(String.class), "%" + pageVO.getRoleName().trim() + "%"));
            }

            Predicate[] p = new Predicate[list.size()];
            return criteriaBuilder.and(list.toArray(p));
        };

        Page<MemberRoleDO> result = memberRoleRepository.findAll(specification, page);

        return Wrapper.success(new PageData<>(result.getTotalElements(), result.getContent().stream().map(r -> {
            PlatformMemberRoleQueryVO queryVO = new PlatformMemberRoleQueryVO();
            queryVO.setId(r.getId());
            queryVO.setRoleName(r.getRoleName());
            queryVO.setRemark(r.getRemark());
            queryVO.setStatus(r.getStatus());
            return queryVO;
        }).collect(Collectors.toList())));
    }

    /**
     * 获取左侧权限菜单列表，以树形菜单形式返回
     * @param headers HttpHeaders信息
     * @return 操作结果
     */
    @Override
    public Wrapper<List<AuthTreeMenuVO>> treeMemberAuth(HttpHeaders headers) {
        UserLoginCacheDTO loginUser = memberCacheService.needLoginFromManagePlatform(headers);
        MemberDO memberDO = memberRepository.findById(loginUser.getMemberId()).orElse(null);
        if (memberDO == null) {
            return Wrapper.fail(ResponseCode.MC_MS_MEMBER_DOES_NOT_EXIST);
        }

        return Wrapper.success(AuthBOUtils.transferToTree(memberDO.getMemberAuth().getAuth()));
    }

    /**
     * 根据左侧菜单Id，查询右侧按钮列表
     *
     * @param headers  HttpHeaders信息
     * @param buttonVO 接口参数
     * @return 操作结果
     */
    @Override
    public Wrapper<List<AuthTreeButtonVO>> getMemberAuthButtons(HttpHeaders headers, AuthTreeGetButtonVO buttonVO) {
        UserLoginCacheDTO loginUser = memberCacheService.needLoginFromManagePlatform(headers);
        MemberDO memberDO = memberRepository.findById(loginUser.getMemberId()).orElse(null);
        if (memberDO == null) {
            return Wrapper.fail(ResponseCode.MC_MS_MEMBER_DOES_NOT_EXIST);
        }

        List<AuthTreeButtonVO> buttonVOList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(memberDO.getMemberAuth().getAuth())) {
            AuthBO authBO = memberDO.getMemberAuth().getAuth().stream().filter(auth -> auth.getId().equals(buttonVO.getMenuId())).findFirst().orElse(null);
            if (authBO == null) {
                return Wrapper.fail(ResponseCode.MC_MS_MEMBER_AUTH_DOES_NOT_EXIST);
            }

            if (!CollectionUtils.isEmpty(authBO.getButtons())) {
                buttonVOList = authBO.getButtons().stream().map(button -> new AuthTreeButtonVO(button.getButtonId(), button.getTitle())).collect(Collectors.toList());
            }
        }

        return Wrapper.success(buttonVOList);
    }

    /**
     * 查询右侧勾选的按钮Id列表
     *
     * @param headers  HttpHeaders信息
     * @param buttonVO 接口参数
     * @return 操作结果
     */
    @Override
    public Wrapper<MemberRoleGetButtonVO> getButtonCheckIds(HttpHeaders headers, MemberRoleButtonVO buttonVO) {
        UserLoginCacheDTO loginUser = memberCacheService.needLoginFromManagePlatform(headers);
        MemberDO memberDO = memberRepository.findById(loginUser.getMemberId()).orElse(null);
        if (memberDO == null) {
            return Wrapper.fail(ResponseCode.MC_MS_MEMBER_DOES_NOT_EXIST);
        }

        AuthBO authBO = memberDO.getMemberAuth().getAuth().stream().filter(auth -> auth.getId().equals(buttonVO.getMenuId())).findFirst().orElse(null);
        if (authBO == null) {
            return Wrapper.fail(ResponseCode.MC_MS_MEMBER_AUTH_DOES_NOT_EXIST);
        }

        MemberRoleGetButtonVO result = new MemberRoleGetButtonVO();
        //查询会员角色的按钮列表，返回checkIds
        List<Long> checkIds = new ArrayList<>();
        MemberRoleDO memberRoleDO = memberRoleRepository.findFirstByMemberAndId(memberDO, buttonVO.getMemberRoleId());
        if (memberRoleDO == null) {
            return Wrapper.fail(ResponseCode.MC_MS_MEMBER_ROLE_DOES_NOT_EXIST);
        }

        if (!CollectionUtils.isEmpty(memberRoleDO.getAuth())) {
            AuthBO roleAuthBO = memberRoleDO.getAuth().stream().filter(a -> a.getId().equals(buttonVO.getMenuId())).findFirst().orElse(null);
            if (roleAuthBO != null) {
                checkIds = roleAuthBO.getButtons().stream().map(ButtonBO::getButtonId).collect(Collectors.toList());
            }
        }

        result.setCheckIds(checkIds);

        return Wrapper.success(result);
    }

    /**
     * 新增会员角色
     *
     * @param headers HttpHeaders信息
     * @param addVO   接口参数
     * @return 操作结果
     */
    @Transactional(rollbackFor = BusinessException.class)
    @Override
    public Wrapper<Void> addMemberRole(HttpHeaders headers, PlatformMemberRoleAddVO addVO) {
        UserLoginCacheDTO loginUser = memberCacheService.needLoginFromManagePlatform(headers);
        MemberDO memberDO = memberRepository.findById(loginUser.getMemberId()).orElse(null);
        if (memberDO == null) {
            return Wrapper.fail(ResponseCode.MC_MS_MEMBER_DOES_NOT_EXIST);
        }


        if (memberRoleRepository.existsByMemberAndRoleName(memberDO, addVO.getRoleName())) {
            return Wrapper.fail(ResponseCode.MC_MS_MEMBER_ROLE_EXISTS);
        }

        List<AuthBO> authBOList = memberDO.getMemberAuth().getAuth();
        if (CollectionUtils.isEmpty(authBOList)) {
            return Wrapper.fail(ResponseCode.MC_MS_MEMBER_AUTH_DOES_NOT_EXIST);
        }

        List<AuthBO> roleAuthList = CollectionUtils.isEmpty(addVO.getMenuIds()) ? new ArrayList<>() : memberDO.getMemberAuth().getAuth().stream().filter(m -> addVO.getMenuIds().contains(m.getId())).collect(Collectors.toList());

        MemberRoleDO memberRoleDO = new MemberRoleDO();
        memberRoleDO.setMember(memberDO);
        memberRoleDO.setTypeEnum(MemberUserAuthTypeEnum.NORMAL.getCode());
        memberRoleDO.setStatus(EnableDisableStatus.ENABLE.getCode());
        memberRoleDO.setAuth(roleAuthList);
        memberRoleDO.setUsers(new HashSet<>());
        memberRoleDO.setRoleName(addVO.getRoleName());
        memberRoleDO.setRemark(StringUtils.hasLength(addVO.getRemark()) ? addVO.getRemark().trim() : "");
        //平台后台不具有IM通信权限和数据权限
        memberRoleDO.setHasImAuth(EnableDisableStatus.DISABLE.getCode());
        memberRoleDO.setAuthConfig(new HashSet<>());

        memberRoleRepository.saveAndFlush(memberRoleDO);

        workflowFeignService.upsertMemberRoleToProcessAsync(loginUser.getMemberId(), loginUser.getMemberRoleId(), memberRoleDO.getRoleName(), roleAuthList);

        return Wrapper.success();
    }

    /**
     * 查询单个会员角色的信息
     *
     * @param headers HttpHeaders信息
     * @param idVO    接口参数
     * @return 操作结果
     */
    @Override
    public Wrapper<PlatformMemberRoleGetVO> getMemberRole(HttpHeaders headers, MemberRoleIdVO idVO) {
        UserLoginCacheDTO loginUser = memberCacheService.needLoginFromManagePlatform(headers);
        MemberDO memberDO = memberRepository.findById(loginUser.getMemberId()).orElse(null);
        if (memberDO == null) {
            return Wrapper.fail(ResponseCode.MC_MS_MEMBER_DOES_NOT_EXIST);
        }

        MemberRoleDO memberRoleDO = memberRoleRepository.findFirstByMemberAndId(memberDO, idVO.getMemberRoleId());
        if (memberRoleDO == null) {
            return Wrapper.fail(ResponseCode.MC_MS_MEMBER_ROLE_DOES_NOT_EXIST);
        }

        PlatformMemberRoleGetVO result = new PlatformMemberRoleGetVO();
        result.setId(memberRoleDO.getId());
        result.setRoleName(memberRoleDO.getRoleName());
        result.setRemark(memberRoleDO.getRemark());
        result.setStatus(memberRoleDO.getStatus());

        //全量权限从会员获得
        List<AuthTreeMenuVO> menuTree = AuthBOUtils.transferToTree(memberDO.getMemberAuth().getAuth());
        result.setAuth(menuTree);

        //勾选的权限从角色本身获得
        List<Long> checkIds = memberRoleDO.getAuth().stream().map(AuthBO::getId).collect(Collectors.toList());
        result.setCheckIds(checkIds);

        return Wrapper.success(result);
    }


    /**
     * 查询会员角色左侧树形权限菜单中勾选的Id列表
     *
     * @param headers HttpHeaders信息
     * @param idVO 接口参数
     * @return 操作结果
     */
    @Override
    public Wrapper<AuthTreeMenuButtonCheckIdVO> getTreeCheckIds(HttpHeaders headers, MemberRoleIdVO idVO) {
        UserLoginCacheDTO loginUser = memberCacheService.needLoginFromManagePlatform(headers);
        MemberDO memberDO = memberRepository.findById(loginUser.getMemberId()).orElse(null);
        if (memberDO == null) {
            return Wrapper.fail(ResponseCode.MC_MS_MEMBER_DOES_NOT_EXIST);
        }

        AuthTreeMenuButtonCheckIdVO result = new AuthTreeMenuButtonCheckIdVO();
        List<Long> checkIds = new ArrayList<>();
        if (idVO.getMemberRoleId() != null && idVO.getMemberRoleId() >= 0L) {
            MemberRoleDO memberRoleDO = memberRoleRepository.findFirstByMemberAndId(memberDO, idVO.getMemberRoleId());
            if (memberRoleDO == null) {
                return Wrapper.fail(ResponseCode.MC_MS_MEMBER_ROLE_DOES_NOT_EXIST);
            }

            if (!CollectionUtils.isEmpty(memberRoleDO.getAuth())) {
                checkIds = memberRoleDO.getAuth().stream().map(AuthBO::getId).collect(Collectors.toList());
            }
        }

        result.setCheckIds(checkIds);
        return Wrapper.success(result);
    }


    /**
     * 更新会员角色及左侧菜单权限
     *
     * @param headers  HttpHeaders信息
     * @param updateVO 接口参数
     * @return 操作结果
     */
    @Override
    @Transactional(rollbackFor = BusinessException.class)
    public Wrapper<Void> updateMemberRole(HttpHeaders headers, PlatformMemberRoleUpdateVO updateVO) {
        UserLoginCacheDTO loginUser = memberCacheService.needLoginFromManagePlatform(headers);
        MemberDO memberDO = memberRepository.findById(loginUser.getMemberId()).orElse(null);
        if (memberDO == null) {
            return Wrapper.fail(ResponseCode.MC_MS_MEMBER_DOES_NOT_EXIST);
        }

        MemberRoleDO memberRoleDO = memberRoleRepository.findFirstByMemberAndId(memberDO, updateVO.getMemberRoleId());
        if (memberRoleDO == null) {
            return Wrapper.fail(ResponseCode.MC_MS_MEMBER_ROLE_DOES_NOT_EXIST);
        }

        //不能更改会员“超级管理员”用户
        if (memberRoleDO.getTypeEnum().equals(MemberUserAuthTypeEnum.ADMIN.getCode())) {
            return Wrapper.fail(ResponseCode.MC_MS_FORBID_DELTE_OR_UPDATE_MEMBER_ROLE);
        }

        if (CollectionUtils.isEmpty(updateVO.getMenuIds())) {
            memberRoleDO.getAuth().clear();
        } else {
            //移除不存在的
            memberRoleDO.getAuth().removeIf(auth -> !updateVO.getMenuIds().contains(auth.getId()));
            //添加新增的
            List<Long> addMenuIds = updateVO.getMenuIds().stream().filter(id -> memberRoleDO.getAuth().stream().noneMatch(role -> role.getId().equals(id))).collect(Collectors.toList());
            List<AuthBO> authBOList = memberDO.getMemberAuth().getAuth().stream().filter(m -> addMenuIds.contains(m.getId())).collect(Collectors.toList());
            memberRoleDO.getAuth().addAll(authBOList);
        }

        memberRoleDO.setRoleName(updateVO.getRoleName());
        memberRoleDO.setRemark(StringUtils.hasLength(updateVO.getRemark()) ? updateVO.getRemark().trim() : "");
        memberRoleRepository.saveAndFlush(memberRoleDO);

        //更新用户权限
        Wrapper<?> result = baseAuthService.updateMemberUserAuthByUpdateMemberRole(memberRoleDO);
        if(result.getCode() != ResponseCode.SUCCESS.getCode()) {
            return Wrapper.fail(result.getCode(), result.getMessage());
        }

        workflowFeignService.upsertMemberRoleToProcessAsync(loginUser.getMemberId(), loginUser.getMemberRoleId(), memberRoleDO.getRoleName(), memberRoleDO.getAuth());

        return Wrapper.success();
    }


    /**
     * 更新会员角色菜单关联按钮
     *
     * @param headers  HttpHeaders信息
     * @param upsertVO 接口参数
     * @return 操作结果
     */
    @Override
    public Wrapper<Void> updateMemberRoleButton(HttpHeaders headers, MemberRoleUpdateButtonVO upsertVO) {
        UserLoginCacheDTO loginUser = memberCacheService.needLoginFromManagePlatform(headers);
        MemberDO memberDO = memberRepository.findById(loginUser.getMemberId()).orElse(null);
        if (memberDO == null) {
            return Wrapper.fail(ResponseCode.MC_MS_MEMBER_DOES_NOT_EXIST);
        }

        MemberRoleDO memberRoleDO = memberRoleRepository.findFirstByMemberAndId(memberDO, upsertVO.getMemberRoleId());
        if (memberRoleDO == null) {
            return Wrapper.fail(ResponseCode.MC_MS_MEMBER_ROLE_DOES_NOT_EXIST);
        }

        //不能更改会员“超级管理员”用户
        if (memberRoleDO.getTypeEnum().equals(MemberUserAuthTypeEnum.ADMIN.getCode())) {
            return Wrapper.fail(ResponseCode.MC_MS_FORBID_DELTE_OR_UPDATE_MEMBER_ROLE);
        }

        AuthBO memberAuthBO = memberDO.getMemberAuth().getAuth().stream().filter(menu -> menu.getId().equals(upsertVO.getMenuId())).findFirst().orElse(null);
        if (memberAuthBO == null) {
            return Wrapper.fail(ResponseCode.MC_MS_MEMBER_AUTH_DOES_NOT_EXIST);
        }

        AuthBO authBO = memberRoleDO.getAuth().stream().filter(menu -> menu.getId().equals(upsertVO.getMenuId())).findFirst().orElse(null);
        if (authBO == null) {
            return Wrapper.fail(ResponseCode.MC_MS_MEMBER_ROLE_MENU_DOES_NOT_EXIST);
        }

        //检验按钮是否存在
        if (CollectionUtils.isEmpty(memberAuthBO.getButtons())) {
            if (!CollectionUtils.isEmpty(upsertVO.getButtonIds())) {
                return Wrapper.fail(ResponseCode.MC_MS_MEMBER_ROLE_BUTTON_DOES_NOT_EXIST);
            }
        } else {
            if (!CollectionUtils.isEmpty(upsertVO.getButtonIds())) {
                if (!upsertVO.getButtonIds().stream().allMatch(buttonId -> memberAuthBO.getButtons().stream().anyMatch(button -> button.getButtonId().equals(buttonId)))) {
                    return Wrapper.fail(ResponseCode.MC_MS_MEMBER_ROLE_BUTTON_DOES_NOT_EXIST);
                }
            }
        }

        if (CollectionUtils.isEmpty(upsertVO.getButtonIds())) {
            authBO.getButtons().clear();
        } else {
            //移除不存在的
            authBO.getButtons().removeIf(button -> !upsertVO.getButtonIds().contains(button.getButtonId()));
            //新增
            List<Long> addButtonIds = upsertVO.getButtonIds().stream().filter(b -> authBO.getButtons().stream().noneMatch(button -> button.getButtonId().equals(b))).collect(Collectors.toList());
            List<ButtonBO> buttons = memberAuthBO.getButtons().stream().filter(button -> addButtonIds.contains(button.getButtonId())).collect(Collectors.toList());
            authBO.getButtons().addAll(buttons);
        }

        memberRoleRepository.saveAndFlush(memberRoleDO);

        Wrapper<?> result = baseAuthService.updateMemberUserAuthByUpdateMemberRole(memberRoleDO);
        if(result.getCode() != ResponseCode.SUCCESS.getCode()) {
            return Wrapper.fail(result.getCode(), result.getMessage());
        }

        return Wrapper.success();
    }

    /**
     * 删除会员角色
     *
     * @param headers HttpHeaders信息
     * @param idVO    接口参数
     * @return 操作结果
     */
    @Override
    public Wrapper<Void> deleteMemberRole(HttpHeaders headers, MemberRoleIdVO idVO) {
        UserLoginCacheDTO loginUser = memberCacheService.needLoginFromManagePlatform(headers);
        MemberDO memberDO = memberRepository.findById(loginUser.getMemberId()).orElse(null);
        if (memberDO == null) {
            return Wrapper.fail(ResponseCode.MC_MS_MEMBER_DOES_NOT_EXIST);
        }

        MemberRoleDO memberRoleDO = memberRoleRepository.findFirstByMemberAndId(memberDO, idVO.getMemberRoleId());
        if (memberRoleDO == null) {
            return Wrapper.fail(ResponseCode.MC_MS_MEMBER_ROLE_DOES_NOT_EXIST);
        }

        //不能更改会员“超级管理员”用户
        if (memberRoleDO.getTypeEnum().equals(MemberUserAuthTypeEnum.ADMIN.getCode())) {
            return Wrapper.fail(ResponseCode.MC_MS_FORBID_DELTE_OR_UPDATE_MEMBER_ROLE);
        }

        //如果已经有用户，不能删除
        if (!CollectionUtils.isEmpty(memberRoleDO.getUsers())) {
            return Wrapper.fail(ResponseCode.MC_MS_MEMBER_ROLE_CANNOT_DELETE_WHEN_USER_EXISTS);
        }

        memberRoleRepository.delete(memberRoleDO);

        workflowFeignService.removeMemberRoleInProcessAsync(loginUser.getMemberId(), idVO.getMemberRoleId());

        return Wrapper.success();
    }

    /**
     * 更新会员角色状态
     *
     * @param headers  HttpHeaders信息
     * @param statusVO 接口参数
     * @return 操作结果
     */
    @Override
    public Wrapper<Void> updateMemberRoleStatus(HttpHeaders headers, MemberRoleUpdateStatusVO statusVO) {
        UserLoginCacheDTO loginUser = memberCacheService.needLoginFromManagePlatform(headers);
        MemberDO memberDO = memberRepository.findById(loginUser.getMemberId()).orElse(null);
        if (memberDO == null) {
            return Wrapper.fail(ResponseCode.MC_MS_MEMBER_DOES_NOT_EXIST);
        }

        MemberRoleDO memberRoleDO = memberRoleRepository.findFirstByMemberAndId(memberDO, statusVO.getId());
        if (memberRoleDO == null) {
            return Wrapper.fail(ResponseCode.MC_MS_MEMBER_ROLE_DOES_NOT_EXIST);
        }

        //不能更改会员“超级管理员”用户
        if (memberRoleDO.getTypeEnum().equals(MemberUserAuthTypeEnum.ADMIN.getCode())) {
            return Wrapper.fail(ResponseCode.MC_MS_FORBID_DELTE_OR_UPDATE_MEMBER_ROLE);
        }

        //如果已经有用户，不能修改状态
        if (!CollectionUtils.isEmpty(memberRoleDO.getUsers())) {
            return Wrapper.fail(ResponseCode.MC_MS_MEMBER_ROLE_CANNOT_DELETE_WHEN_USER_EXISTS);
        }

        memberRoleDO.setStatus(statusVO.getStatus());
        memberRoleRepository.saveAndFlush(memberRoleDO);

        return Wrapper.success();
    }
}
